// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
// 'starter.services' is found in services.js
// 'starter.controllers' is found in controllers.js
angular.module('starter', ['ui.router', 'starter.controllers'], function ($httpProvider) {

    //此方法用于修改$http请求的数据格式，让后台可以按照ajax，传输数据的方式接收
    // Use x-www-form-urlencoded Content-Type
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';

    /**
     * The workhorse; converts an object to x-www-form-urlencoded serialization.
     * @param {Object} obj
     * @return {String}
     */
    var param = function (obj) {
        var query = '', name, value, fullSubName, subName, subValue, innerObj, i;

        for (name in obj) {
            value = obj[name];

            if (value instanceof Array) {
                for (i = 0; i < value.length; ++i) {
                    subValue = value[i];
                    fullSubName = name + '[' + i + ']';
                    innerObj = {};
                    innerObj[fullSubName] = subValue;
                    query += param(innerObj) + '&';
                }
            }
            else if (value instanceof Object) {
                for (subName in value) {
                    subValue = value[subName];
                    fullSubName = name + '[' + subName + ']';
                    innerObj = {};
                    innerObj[fullSubName] = subValue;
                    query += param(innerObj) + '&';
                }
            }
            else if (value !== undefined && value !== null)
                query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
        }

        return query.length ? query.substr(0, query.length - 1) : query;
    };

    // Override $http service's default transformRequest
    $httpProvider.defaults.transformRequest = [function (data) {
        return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data;
    }];
})
    .run(function($rootScope, $transitions, $state,$stateParams) {
        $rootScope.logisticslist = ['sf','tc'];
        $rootScope.logistics = 'zh';
        $rootScope.backToUpPage = function(){
            window.history.back()
        }
        $rootScope.selectState = function(type){
            var logistics = $rootScope.logisticslist[type];
            $rootScope.logisticslist[type] = $rootScope.logistics;
            $rootScope.logistics = logistics;
        }


        $transitions.onStart({
            to: function(state) {
                if((['tab.home', 'tab.discovery', 'tab.order', 'tab.about']).indexOf(state.name) > -1){
                        $rootScope.firstPage = true;
                }else {
                    $rootScope.firstPage = false;
                    $rootScope.others = [];
                }
            }
        })
    })


    .config(function ($stateProvider, $urlRouterProvider) {
        FastClick.attach(document.body)
        $stateProvider
        // setup an abstract state for the tabs directive
            .state('tab', {
                url: '/tab',
                abstract: true,
                templateUrl: 'templates/tabs.html'
            })
            // Each tab has its own nav history stack:
            //首页
            .state('tab.home', {
                url: '/home',
                templateUrl: 'templates/tab-home.html',
                controller: 'HomeCtrl'
            })
            //发现
            .state('tab.discovery', {
                url: '/discovery',
                templateUrl: 'templates/tab-discovery.html',
                controller: 'DiscoveryCtrl'
            })
            //文章详情页面
            .state("tab.findDetail", {
                url: '/discovery/:id',
                templateUrl: 'templates/tab-findDetail.html',
                controller: 'FindDetailCtrl',
                params:{"id":null}
            })


            //订单
            .state('tab.order', {
                url: '/order',
                templateUrl: 'templates/tab-order.html',
                controller: 'OrderCtrl'
            })
            //我的
            .state('tab.about', {
                url: '/about',
                templateUrl: 'templates/tab-about.html',
                controller: 'AboutCtrl'
            })
            //消息盒子
            .state('tab.massage', {
                url: '/massage',
                templateUrl: 'templates/tab-massage.html',
                controller: 'MassageCtrl'
            })
            //设置
            .state('tab.set', {
                url: '/set',
                templateUrl: 'templates/tab-set.html',
                controller: 'SetCtrl'
            })
            //账号相关答疑
            .state('tab.QandA', {
                url: '/QandA',
                templateUrl: 'templates/tab-QandA.html',
                controller: 'QandACtrl'
            })
            //账号相关答疑
            .state('tab.msgTip', {
                url: '/msgTip',
                templateUrl: 'templates/tab-msgTip.html',
                controller: 'MsgTipCtrl'
            })
            //账号相关答疑
            .state('tab.latelyLogin', {
                url: '/latelyLogin',
                templateUrl: 'templates/tab-latelyLogin.html',
                controller: 'LatelyLoginCtrl'
            })
            //我的账号
            .state('tab.myAccount', {
                url: '/myAccount',
                templateUrl: 'templates/tab-myAccount.html',
                controller: 'MyAccountCtrl'
            })
            //注册---用户名
            .state('tab.registerName', {
                url: '/registerName?titleName',
                templateUrl: 'templates/tab-registerName.html',
                controller: 'RegisterNameCtrl'
            })
            //修改密码
            .state('tab.changePassword', {
                url: '/changePassword',
                templateUrl: 'templates/tab-changePassword.html',
                controller: 'ChangePasswordCtrl'
            })

        // if none of the above states are matched, use this as the fallback
        $urlRouterProvider.otherwise('/tab/home');
    });

